﻿namespace Blaze.Editor
{
    using EditorOnly;
    using Framework;
    using Framework.Utilities;
    using UnityEditor;
    using UnityEditor.SceneManagement;
    using UnityEngine;

    /// <summary>
    /// 初次使用向导窗口。
    /// </summary>
    public class StartupWizardWindow : EditorWindow
    {
        [MenuItem("Blaze/面板/使用向导", false, BlazeEditorMenu.Priority.Panels)]
        public static void Open()
        {
            var window = GetWindow<StartupWizardWindow>(false, "向导", true);
        }

        private void init()
        {
            FileUtility.EnsureDirectory(ProjectPath.Get("SdkAssets/Default"));
            FileUtility.EnsureDirectory(ProjectPath.Get("SdkPlugins/Default"));
            FileUtility.EnsureDirectory(ProjectPath.Get("Configs/ClientData"));
            FileUtility.EnsureDirectory(ProjectPath.Get("Configs/ServerData"));
            FileUtility.EnsureDirectory(ProjectPath.Get("RawAssets"));
            FileUtility.EnsureDirectory(ProjectPath.Get("Lua"));
            FileUtility.EnsureDirectory(ProjectPath.GetAssets("Resources/Scenes"));
            FileUtility.EnsureDirectory(ProjectPath.GetAssets("Resources(AssetBundle)"));
            FileUtility.EnsureDirectory(ProjectPath.GetAssets("SplashScreens/Default"));
            AssetDatabase.Refresh();

            var bootstrapScene = AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Scenes/Bootstrap.unity");
            if (bootstrapScene == null)
            {
                var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
                var globalObj = new GameObject("Global");
                globalObj.AddComponent<DontDestroyOnLoad>();

                var bootstrapObj = new GameObject("Bootstrap");
                bootstrapObj.AddComponent<Bootstrap>();
                bootstrapObj.transform.SetParent(globalObj.transform);
                EditorSceneManager.SaveScene(scene, "Assets/Resources/Scenes/Bootstrap.unity");
            }

            var splashScreenScene = AssetDatabase.LoadMainAssetAtPath("Assets/SplashScreens/Default/SplashScreen.unity");
            if (splashScreenScene == null)
            {
                var scene = EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single);
                EditorSceneManager.SaveScene(scene, "Assets/SplashScreens/Default/SplashScreen.unity");
            }
        }

        private void OnGUI()
        {
            if (GUILayout.Button("初始化目录"))
            {
                init();
            }
        }
    }
}